//
//  ViewController.m
//  UI10_UITabView_plist文件
//
//  Created by LGC on 16/3/22.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TabViewOfContact.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

// 获取字典中所有keys.
@property (nonatomic, retain) NSArray *arrOfKeys;

@property (nonatomic, retain) NSDictionary *dic;
@end

@implementation ViewController

- (void)dealloc {
    [_arrOfKeys release];
    [_dic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createTabView];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 知识点1:plist文件的使用.

- (void)handleData {
    
    // 读取plist文件(从磁盘读到内存).

    NSString *path = [[NSBundle mainBundle]pathForResource:@"Contacts" ofType:@"plist"];
    self.dic  = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"%@", self.dic);
    
    // 处理数据, 将key值进行升序排序.
    self.arrOfKeys = [[self.dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", self.arrOfKeys);
}



#pragma mark - tableView
- (void)createTabView {
    
    TabViewOfContact *contact = [[TabViewOfContact alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    contact.delegate = self;
    contact.dataSource = self;
    
    [self.view addSubview:contact];
    
    [contact release];
    
}

#pragma mark - tableView DataSource
/** 返回section(分区)数. */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.arrOfKeys.count;
    
}
/** 返回row数. */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSString *key = [self.arrOfKeys objectAtIndex:section];
    
    NSArray *array = [self.dic objectForKey:key];
    
    return array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *pool1 = @"pool";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pool1];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:pool1]autorelease];
    }
    
    // cell 赋值.
    // 找到key.
    NSString *key = [self.arrOfKeys objectAtIndex:indexPath.section];
    
    // 根据key找到对应的数组.
    NSArray *arr = [self.dic objectForKey:key];
    
    // 找到联系人小字典.
    NSDictionary *smallDic = [arr objectAtIndex:indexPath.row];

    cell.textLabel.text = [smallDic objectForKey:@"name"];
    
    cell.detailTextLabel.text = [smallDic objectForKey:@"phoneNumber"];
    
    
    return cell;
    
}

/** 右侧索引栏. */
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    return self.arrOfKeys;
}

/** 分区头部标题. */
- (NSString * _Nullable)tableView:(UITableView * _Nonnull)tableView titleForHeaderInSection:(NSInteger)section {
    
    return [self.arrOfKeys objectAtIndex:section];
    
}

#pragma mark - tableViewDelegate
- (void)tableView:(UITableView * _Nonnull)tableView didSelectRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath {
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
